﻿using System;
using DDDProjectSample.Domain.Services;
using System.Net.Mail;

namespace DDDProjectSample.Infrastructure.Services
{
    public class SmtpMailSender : IEMailSender
    {
        private string _smtpServer;

        /// <summary>
        /// Creates a new instance of class SmtpMailSender
        /// </summary>
        /// <param name="smtpServer">SMTP server to use for mail delivery</param>
        public SmtpMailSender(string smtpServer)
        {
            _smtpServer = smtpServer;
        }

        #region IEMailSender Members

        /// <summary>
        /// Sends an email
        /// </summary>
        /// <param name="from">Email adress of the sender</param>
        /// <param name="to">Email adress of the receiver</param>
        /// <param name="subject">Email's subject</param>
        /// <param name="body">Email's content</param>
        public void SendMail(string from, string to, string subject, string body)
        {
            // Create mail
            MailMessage email = new MailMessage();
            email.From = new MailAddress(from);
            email.To.Add(new MailAddress(to));
            email.Subject = subject;
            email.Body = body;

            // Configure SMTP Server to use
            SmtpClient client = new SmtpClient();
            client.Host = _smtpServer;

            // Send mail
            client.Send(email);
        }

        #endregion
    }
}
